//
//  YMToolBarView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/26.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

enum YMToolBarViewButtonType:Int {
    case Picture = 1001
    case Mention = 1002
    case Trend = 1003
    case Emoticon = 1004
    case Add = 1005
}


class YMToolBarView: UIView {
    
    
    //表情按钮
    var emoticonButton:UIButton?
    //表情键盘是否弹出
    var isEmoticon: Bool = false{
        didSet{
           let imgName = isEmoticon ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            emoticonButton?.setImage(UIImage(named: imgName), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
            
        }
    }
    
    
    
    
    
    
    
    var closureButton:((type:YMToolBarViewButtonType) ->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI() {

        // 添加控件
        let pictureButton = addChildButtonToSelf("compose_toolbar_picture",type:.Picture)
        let mentionButton = addChildButtonToSelf("compose_mentionbutton_background",type:.Mention)
        let trendButton = addChildButtonToSelf("compose_trendbutton_background",type:.Trend)
         emoticonButton = addChildButtonToSelf("compose_emoticonbutton_background",type:.Emoticon)
        let addButton = addChildButtonToSelf("compose_add_background",type:.Add)
        
        // 添加约束
        pictureButton.snp_makeConstraints { (make) in
        make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton!)
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
        }
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
      
    }
    
    //        MARK: - 点击事件
    
    func didClickButtonAction(button: UIButton) {
        
        closureButton?(type: YMToolBarViewButtonType(rawValue:button.tag)!)
    }
  
    
    
    
    //公共方法
     func addChildButtonToSelf(imgName:String,type:YMToolBarViewButtonType) ->UIButton{
        
        let button = UIButton()
        button.addTarget(self, action: #selector(YMToolBarView.didClickButtonAction(_:)), forControlEvents: .TouchUpInside)
        // rawValue 通过rawValue 把枚举名 转成枚举int数值
        button.tag = type.rawValue
        // 添加点击事件
//        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named:imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        addSubview(button)
        return button
    }
    
}
